﻿package com.projects.xenia.prompts_activities;

import com.projects.xenia.R;
import com.projects.xenia.R.id;
import com.projects.xenia.R.layout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Activity_NumericPrompt extends Activity {

	private Button Prompt_Numeric_One;
	private Button Prompt_Numeric_Two;
	private Button Prompt_Numeric_Three;
	private Button Prompt_Numeric_Four;
	private Button Prompt_Numeric_Five;
	private Button Prompt_Numeric_Six;
	private Button Prompt_Numeric_Seven;
	private Button Prompt_Numeric_Eight;
	private Button Prompt_Numeric_Nine;
	private Button Prompt_Numeric_Null;	
	private Button Prompt_Numeric_Point;
	private Button Prompt_Numeric_Enter;
	private Button Prompt_Numeric_Clear;	
	private Button Prompt_Numeric_BackSpace;
	private TextView Prompt_Numeric_Numeric; 
	
	private Double NumericValue;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);   
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);           
        
        setContentView(R.layout.prompt_numeric);
        
        Prompt_Numeric_One = (Button)findViewById(R.id.Prompt_Numeric_One);
        Prompt_Numeric_One.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Two = (Button)findViewById(R.id.Prompt_Numeric_Two);
        Prompt_Numeric_Two.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Three = (Button)findViewById(R.id.Prompt_Numeric_Three);
        Prompt_Numeric_Three.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Four = (Button)findViewById(R.id.Prompt_Numeric_Four);
        Prompt_Numeric_Four.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Five = (Button)findViewById(R.id.Prompt_Numeric_Five);
        Prompt_Numeric_Five.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Six = (Button)findViewById(R.id.Prompt_Numeric_Six);
        Prompt_Numeric_Six.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Seven = (Button)findViewById(R.id.Prompt_Numeric_Seven);
        Prompt_Numeric_Seven.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Eight = (Button)findViewById(R.id.Prompt_Numeric_Eight);
        Prompt_Numeric_Eight.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Nine = (Button)findViewById(R.id.Prompt_Numeric_Nine);
        Prompt_Numeric_Nine.setOnClickListener(Prompt_Numeric_Number_Click);

        Prompt_Numeric_Null = (Button)findViewById(R.id.Prompt_Numeric_Null);
        Prompt_Numeric_Null.setOnClickListener(Prompt_Numeric_Number_Click);
        
        Prompt_Numeric_Point = (Button)findViewById(R.id.Prompt_Numeric_Point);
        Prompt_Numeric_Point.setOnClickListener(Prompt_Numeric_Number_Point); 
        
        Prompt_Numeric_Clear = (Button)findViewById(R.id.Prompt_Numeric_Clear);
        Prompt_Numeric_Clear.setOnClickListener(Prompt_Numeric_Clear_Click);

        Prompt_Numeric_BackSpace = (Button)findViewById(R.id.Prompt_Numeric_BackSpace);
        Prompt_Numeric_BackSpace.setOnClickListener(Prompt_Numeric_BackSpace_Click);
        
        Prompt_Numeric_Enter = (Button)findViewById(R.id.Prompt_Numeric_Enter);
        Prompt_Numeric_Enter.setOnClickListener(Prompt_Numeric_Enter_Click);		

        Prompt_Numeric_Numeric = (TextView)findViewById(R.id.Prompt_Numeric_Numeric);       
        
        
        Prompt_Numeric_Numeric.setInputType(InputType.TYPE_NULL);
        Prompt_Numeric_Numeric.setFocusable(true);
                
        Bundle DocExtras;
        DocExtras = getIntent().getExtras();
        
        NumericValue = DocExtras.getDouble("Numeric");        
        Prompt_Numeric_Numeric.setText(String.valueOf(NumericValue));
        Prompt_Numeric_Numeric.setSelected(true);
        Prompt_Numeric_Numeric.requestFocus();        
        
	}

	private OnClickListener Prompt_Numeric_Number_Click = new OnClickListener() {
		@Override
		public void onClick(View v) {

			Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			vb.vibrate(30);

			Button NumberButton = (Button)v;
			String NumberButtonText = (String) NumberButton.getText();
						
			String FocusedTextViewText = (String) Prompt_Numeric_Numeric.getText().toString();
			
			if (Prompt_Numeric_Numeric.isSelected()) {
				FocusedTextViewText = "";
				Prompt_Numeric_Numeric.setSelected(false);				
			}			
			
			if (FocusedTextViewText.equals("0")) {
				Prompt_Numeric_Numeric.setText(NumberButtonText);
			}
			else {
				Prompt_Numeric_Numeric.setText(FocusedTextViewText.concat(NumberButtonText));
			}			
			
		}
	};
	
	private OnClickListener Prompt_Numeric_Number_Point = new OnClickListener() {
		@Override
		public void onClick(View v) {

			Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			vb.vibrate(30);

			Button NumberButton = (Button)v;
			String NumberButtonText = (String) NumberButton.getText();
			
			String FocusedTextViewText = (String) Prompt_Numeric_Numeric.getText().toString();

			if (Prompt_Numeric_Numeric.isSelected()) {
				FocusedTextViewText = "0.";
				Prompt_Numeric_Numeric.setSelected(false);	
				return;
			}	
			
			if (FocusedTextViewText.contains(".")!=true) {
				Prompt_Numeric_Numeric.setText(FocusedTextViewText.concat(NumberButtonText));					
			}
			
		}
	};
	
	private OnClickListener Prompt_Numeric_Clear_Click = new OnClickListener() {
		@Override
		public void onClick(View v) {		
		
			Prompt_Numeric_Numeric.setText("0");			
			
		}
	};
	
	
	private OnClickListener Prompt_Numeric_BackSpace_Click = new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			String FocusedTextViewText = Prompt_Numeric_Numeric.getText().toString();
			
			if (Prompt_Numeric_Numeric.isSelected()) {
				FocusedTextViewText = "";
				Prompt_Numeric_Numeric.setSelected(false);
			}
			
			if (FocusedTextViewText.length() > 1) {
				Prompt_Numeric_Numeric.setText(FocusedTextViewText.substring(0, FocusedTextViewText.length()-1));	
			}
			else {
				Prompt_Numeric_Numeric.setText("0");
			}			
			
		}
	};

	private OnClickListener Prompt_Numeric_Enter_Click = new OnClickListener() {
		@Override
		public void onClick(View v) {	
			
			NumericValue = Double.parseDouble(Prompt_Numeric_Numeric.getText().toString());
			
			Intent DataIntent = new Intent ();			
			DataIntent.putExtra("NumericValue",NumericValue);			
			setResult(RESULT_OK, DataIntent);
			finish();	
			
		}
	};
	
	
	
}
